/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package hideme;

import java.io.IOException;
import java.util.Enumeration;
import java.util.Vector;
import javax.microedition.io.Connector;
import javax.microedition.io.file.FileConnection;
import javax.microedition.io.file.FileSystemRegistry;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.List;

/**
 *
 * @author Eduard
 */
public class HideMeBrowser extends List implements CommandListener {

    private final static String ROOT = "/";
    private final static char SEPARATOR = '/';
    private final static String FOLDER_UP = "../";
    private Image dirIcon = null, fileIcon = null;
    public static final Command SELECT_FILE_COMMAND = new Command("Select", Command.OK, 1);
    public static final Command SELECT_FOLDER_COMMAND = new Command("Use folder", Command.OK, 2);
    private final Vector path = new Vector();
    private CommandListener commandListener = null;

    public HideMeBrowser(){
        super("", IMPLICIT);
        super.setCommandListener(this);
        setSelectCommand(SELECT_FILE_COMMAND);
        try {
            dirIcon = Image.createImage("/org/netbeans/microedition/resources/dir.png");
        } catch (IOException e) {}
        try {
            fileIcon = Image.createImage("/org/netbeans/microedition/resources/file.png");
        } catch (IOException e) { }
        fillList();
    }

    public String getCurrentFolder(){
        String ret = "/";
        for(int i = 0; i < path.size(); i++){
            ret += path.elementAt(i);
        }
        return ret;
    }

    public String getCurrentFile(){
        return getString(getSelectedIndex());
    }

    private void fillList(){
        new Thread(new Runnable(){
            public void run() {
                String folder = getCurrentFolder();
                if(folder.equals(ROOT)){//Don't allow to select ROOT
                    removeCommand(SELECT_FOLDER_COMMAND);
                }else{
                    addCommand(SELECT_FOLDER_COMMAND);
                }
                setTitle(folder);
                deleteAll();
                Enumeration e = null;
                FileConnection currDir = null;
                try {
                    if(folder.equals(ROOT)){
                        e = FileSystemRegistry.listRoots();
                    }else{
                        append(FOLDER_UP, dirIcon);
                        currDir = (FileConnection) Connector.open("file://" + folder);
                        e = currDir.list();
                    }
                    while (e.hasMoreElements()) {
                        String fileName = (String) e.nextElement();
                        if (isFolder(fileName)){
                            append(fileName, dirIcon);
                        } else {
                            append(fileName, fileIcon);
                        }
                    }
                } catch (IOException ex) {
                    throw new RuntimeException(ex.getMessage());
                }finally{
                    if(currDir != null) try{
                        currDir.close();
                    } catch (IOException ex) {
                        throw new RuntimeException(ex.getMessage());
                    }
                }
            }

        }).start();
    }

    private boolean isFolder(String name){
        return name.charAt(name.length() - 1) == SEPARATOR;
    }

    public void setCommandListener(CommandListener commandListener){
        this.commandListener = commandListener;
    }

    public CommandListener getCommandListener(){
        return commandListener;
    }
    public void commandAction(Command c, Displayable d) {
        if(c == SELECT_FILE_COMMAND){
            String selection = getString(getSelectedIndex());
            if(FOLDER_UP.equals(selection)){
                path.removeElementAt(path.size()-1);
                fillList();
            }else if(isFolder(selection)){
                path.addElement(selection);
                fillList();
            }else{
                commandListener.commandAction(c, d);
            }
        }else{
            if(commandListener != null){
               commandListener.commandAction(c, d);
               fillList();
            }
        }
    }

}
